<?php


class Crawl_keyword_from_api_library  extends Crawl_keyword_library{

    private $_parameters = '';
    public  $_platform;
    /**
     * 定义 CURL 消息头、用户端信息、抓取地址
     * 定义 新浪网页抓取选项
     */
    function __construct(){
        parent::__construct();
        $openplatforms = $this->_ci->config->item('openplatforms');
        $this->_aip_search_config  = $openplatforms['openplatforms']['apisearch'][ $this->_platform];
        $this->_app_info_config = $openplatforms['openplatforms']['app_info'][ $this->_platform];

        $this->write_log_stary_tag();
    }


    public function crawl($retry = FALSE) {
        $crawl_result = $this->try_crawl($retry);

        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        } elseif(is_null($crawl_result)) {
            return array('data' => NULL);
        }

        return $this->make_data($crawl_result);
    }

    private function make_data($result = NULL) {
        $this->_ci->load->model('split/Apisearch_parser_model');
        $parser_search_sohu_api_function = 'parser_search_'.$this->_platform.'_api';
        $split_data = $this->_ci->Apisearch_parser_model->$parser_search_sohu_api_function($this->_aip_search_config['source_myid'], $result);
        if($split_data == FALSE) {
            log_message(self::LOG_LEVEL_WARING, 'Match And Split Data Failed!', FALSE, $this->_log_dir);
        }

        return $split_data;
    }

    /**
     * 尝试抓取
     * @param bool $retry 是否使用代理和重试功能
     * @return array|string 错误时返回error数组 正确时返回抓取到的字符穿
     */
    private function try_crawl($retry = FALSE) {
        $parameters = $this->make_parameters();
        if($retry == FALSE) {
            //不使用代理、不重试直接抓取
            log_message(self::LOG_LEVEL_NOTICE, 'Will Crawl From '.$this->_platform.' API Use Retry And Un Use Proxy With Parameters: '.json_encode($parameters), FALSE, $this->_log_dir);
            return self::get_content($parameters);
        } else {
            $crawl_use_proxy_ip_result = $this->crawl_use_proxy_ip($this->_parameters, self::PRROXY_IP, $this->_crawl_from[$this->_platform.'_api'], FALSE);
            if(is_array($crawl_use_proxy_ip_result) && array_key_exists($this->_error_string, $crawl_use_proxy_ip_result)) {
                return $this->crawl_use_proxy_ip($this->_parameters, self::MOBILITY_IP, $this->_crawl_from[$this->_platform.'_api'], FALSE);
            } else {
                return $crawl_use_proxy_ip_result;
            }
        }
    }

    public function process_crawl_result($crawl_result, $use_proxy = self::PRROXY_IP) {
        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        }

        $crawl_result_array = $this->change_code($crawl_result['result']);
        if($crawl_result_array == FALSE) {
            $error = $this->_ci->error_code_library->getError('system', 'data', 'decode', $crawl_result);
            log_message(self::LOG_LEVEL_NOTICE, json_encode($error), FALSE, $this->_log_dir);
            return $error;
        }

        //腾讯错误 ret != 0 搜狐错误 存在error
        if((isset($crawl_result_array['ret']) && $crawl_result_array['ret'] != 0) || array_key_exists('error', $crawl_result_array)){
            $error = $this->_ci->error_code_library->getError('system', 'data', 'return_error', $crawl_result['result']);
            log_message(self::LOG_LEVEL_WARING, json_encode($error), FALSE, $this->_log_dir);
            return $error;
        }

        if(array_key_exists('data', $crawl_result_array) && is_null($crawl_result_array['data']) || (array_key_exists('statuses', $crawl_result_array) && is_array($crawl_result_array['statuses']) && count($crawl_result_array['statuses']) < 1)) {
            return NULL;
        }
        return $crawl_result_array;
    }

    private function make_parameters() {
        $user_token = $this->get_user_token($this->_platform);
        if(array_key_exists($this->_error_string, $user_token)) {
            return $user_token;
        }

        $this->update_user_token_total($user_token['id']);
        if(isset($this->_app_info_config[$user_token['appkey']])) {
            $app_key_info = $this->_app_info_config[$user_token['appkey']];
        } else {
            $error = $this->_ci->error_code_library->getError('token', 'data', 'no_appkey', json_encode($user_token));
            log_message(self::LOG_LEVEL_WARING, $error, FALSE, $this->_log_dir);
            return $error;
        }

        $app_info = array(
                    'app_info' => $app_key_info,
                    'token_info' => $user_token,
        );

        $page = $this->get_page();
        $keyword = $this->_ci->input->post('word');

        $this->_ci->load->library('client/'.$this->_platform.'_client', $app_info);
        $library = $this->_platform.'_client';

        //腾讯平台  传入starttime参数..只抓取上次抓取后的数据
        if($this->_platform == 'tencent') {
        	$starttime = intval($this->_ci->input->post('starttime'));
        	$parameters = $this->_ci->$library->search($keyword,$page,$starttime);
        }else{
        	$parameters = $this->_ci->$library->search($keyword, $page);
        }

        return $this->_parameters = $parameters;
    }

}